#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
闲云潭影日悠悠, 物转星移几度秋!
						--王勃
"""
from pyse import Pyse

CSS_SUBMIT = "id=>J_SubmitQuick"
CSS_USER_FORMAT = "css=>#J_QuickLogin > form > ul.userlist > li:nth-child(%s) > label"
URL='https://login.taobao.com/member/login.jhtml?style=mini&from=alimama'
TITLE = u'阿里妈妈 大数据营销平台'

def getAlimamaCookies(user):
	driver = Pyse("ie")
	driver.open(URL)

	driver.element_wait("css", ".userlist", 5)

	cookies = None
	n = 1
	while True:
		try:
			text = driver.get_text(CSS_USER_FORMAT % n)
			if text == user:
				driver.click( CSS_USER_FORMAT % n )
				driver.submit(CSS_SUBMIT)
				if driver.title_contains(TITLE, 10):
					cookies = driver.get_cookies()
				else:
					driver.F5()
					driver.click( CSS_USER_FORMAT % n )
					driver.submit(CSS_SUBMIT)
					driver.title_contains(TITLE, 10)
					cookies = driver.get_cookies()
				break
			n += 1
		except Exception as e:
			break
	driver.close()
	return cookies


if __name__ == '__main__':
	user = u'苦哈2009'
	print getAlimamaCookies(user)